// pages/techordermg/techordermg.js
var api = require('../../config/api.js')
var comm = require("../../utils/comm.js")
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 'tab0',

    myAgentList: {
      hasMore: true,
      list: [],
      list2: [],
      listSize: 0
    },
    startNum: 1, //页数
    total: '0',

    tokenType:undefined,

  },

  orderinfo: function (e) {
    var that = this
    wx.navigateTo({
      url: '/pages/techordermg/techorderinfo/techorderinfolis?id=' + e.currentTarget.id,
    })
  },

  handleChange({ detail }) {
    var that = this
    that.setData({
      current: detail.key
    });
    console.log(that.data.current)
    var status = '';
    if (that.data.current == 'tab0') {
      status = '0';
    } else if (that.data.current == 'tab1') {
      status = '1';
    } else if (that.data.current == 'tab2') {
      status = '2';
    } else if (that.data.current == 'tab3') {
      status = '3';
    }

    var _url = api.OrdersList+ '?current=1&size=5';
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: _url,
      data: {
        status: status,
        tokenType: that.data.tokenType
      },
      method: 'POST',
      header: {
        'Accept': 'application/json',
        'ACCESS-TOKEN': wx.getStorageSync('token')
      },
      success: function (res) {
        console.log(res.data)
        wx.hideLoading();
        if (res.data.code == '1') {
          var listSize = res.data.obj.records.length;
          console.log('listSize=' + listSize);
          var newData = that.data.myAgentList.list;
          newData = res.data.obj.records;
          var total1 = res.data.obj.total;
          var newList = {};
          //当长度小于10表示没有下页数据了
          if (listSize < 5) {
            newList.hasMore = false;
          } else {
            newList.hasMore = true;
          }
          newList.list = newData;
          console.log('newData=' + newData);
          newList.listSize = listSize;
          that.setData({
            myAgentList: newList,
            total: total1
          });

          console.log('myAgentList=' + that.data.myAgentList);

          that.setData({
            startNum: 2
          })
        } else {
          // util.msg("错误", res.data.msg)
          console.log("网络通，数据不成功")
        }
      },
      fail: function (res) {
        wx.hideLoading();
        console.log(res.data)
        // util.msg("错误", "通讯失败")
      }
    })

  },

  //下拉刷新
  hotActivityLoadMore: function (e) {
    var _that = this;
    //判断是否还有下一页
    if (_that.data.myAgentList.hasMore == true) {
      wx.showLoading({
        title: '加载中',
      })
      var status = '';
      if (_that.data.current == 'tab0') {
        status = '0';
      } else if (_that.data.current == 'tab1') {
        status = '1';
      } else if (_that.data.current == 'tab2') {
        status = '2';
      } else if (_that.data.current == 'tab3') {
        status = '3';
      }

      var pageNum = _that.data.startNum
      var _url = 'current=' + pageNum + '&size=5';

      comm.request(api.OrdersList + _url, { status: status, tokenType: _that.data.tokenType }, "POST").then(function (res) {
        wx.hideLoading();
        if (res.code == '1') {
          var listSize = res.obj.records.length;
          console.log('listSize=' + listSize);
          var newData = _that.data.myAgentList.list;
          var lis = res.obj.records;
          var total1 = res.obj.total;
          //concat是在原有基础上进行增加
          newData = newData.concat(lis);
          var newList = {};
          //当长度小于5表示没有下页数据了
          if (listSize < 5) {
            wx.showToast({
              title: "已加载全部列表",
              icon: 'none',
              duration: 2000
            });
            newList.hasMore = false;
          } else {
            newList.hasMore = true;
          }
          newList.list = newData;
          newList.listSize = listSize;
          _that.setData({
            myAgentList: newList,
            total: total1
          });

          _that.setData({
            startNum: pageNum + 1 //页数加1
          })
        } else {
          // util.msg("错误", res.data.msg)
          console.log("网络不通，数据不成功")
        }

      }).catch(function (res) {

        console.log("请求失败！")
      })
    } else {
      wx.showToast({
        title: "已加载全部商品",
        icon: 'none',
        duration: 2000
      });
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //加载页面
    var _that = this

    var usertype = wx.getStorageSync('usertype');
    if (usertype == '4'){
      _that.setData({
        tokenType: 3
      })
    } else if (usertype == '5'){
      _that.setData({
        tokenType: 2
      })
    }



    var status = '';
    if (_that.data.current == 'tab0') {
      status = '0';
    } else if (_that.data.current == 'tab1') {
      status = '1';
    } else if (_that.data.current == 'tab2') {
      status = '2';
    } else if (_that.data.current == 'tab3') {
      status = '3';
    }

    var _url = api.OrdersList + '?current=1&size=5';
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: _url,
      data: {
        status: status,
        tokenType: _that.data.tokenType
      },
      method: 'POST',
      header: {
        'Accept': 'application/json',
        'ACCESS-TOKEN': wx.getStorageSync('token')
      },
      success: function (res) {
        console.log(res.data)
        wx.hideLoading();
        if (res.data.code == '1') {
          var listSize = res.data.obj.records.length;
          console.log('listSize=' + listSize);
          var newData = _that.data.myAgentList.list;
          newData = res.data.obj.records;
          var total1 = res.data.obj.total;
          var newList = {};
          //当长度小于10表示没有下页数据了
          if (listSize < 5) {
            newList.hasMore = false;
          } else {
            newList.hasMore = true;
          }
          newList.list = newData;
          console.log('newData=' + newData);
          newList.listSize = listSize;
          _that.setData({
            myAgentList: newList,
            total: total1
          });

          console.log('myAgentList=' + _that.data.myAgentList);

          _that.setData({
            startNum: 2
          })
        } else {
          // util.msg("错误", res.data.msg)
          console.log("网络不通，数据不成功")
        }
      },
      fail: function (res) {
        wx.hideLoading();
        console.log(res.data)
        // util.msg("错误", "通讯失败")
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onLoad();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})